package org.demo.url;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Created by tangwy on 2016/10/17.
 */
public class MutilDownload {
    private final static int DEFAULT_THREAD_POOL_SIZE=4;
    //private DownloadRunable[]tasks;
    //private ThreadPoolExecutor executor;
    private int threadPoolSize;
    private String filePath;
    private InputStream[]isArr;
    private URL url;
    public MutilDownload(URL url,String filePath){
        this(url,DEFAULT_THREAD_POOL_SIZE,filePath);
    }
    public MutilDownload(URL url,int threadPoolSize,String filePath){
        if(url==null){
            throw new NullPointerException("url is null");
        }
        if(filePath==null){
            throw new NullPointerException("filePath is null");
        }
        if(filePath.length()==0){
            throw new IllegalArgumentException("filePath is empty");
        }
        this.url=url;
        this.filePath=filePath;
        if(threadPoolSize<=0||threadPoolSize>DEFAULT_THREAD_POOL_SIZE){
            this.threadPoolSize=DEFAULT_THREAD_POOL_SIZE;
        }else {
            this.threadPoolSize=threadPoolSize;
        }
        isArr=new InputStream[this.threadPoolSize];
        /*executor=new ThreadPoolExecutor(threadPoolSize,threadPoolSize,10L,
                TimeUnit.SECONDS,new ArrayBlockingQueue<Runnable>(DEFAULT_THREAD_POOL_SIZE*2));*/
    }
    public MutilDownload(String urlPath, String filePath)
            throws MalformedURLException {
        this(new URL(urlPath),DEFAULT_THREAD_POOL_SIZE,filePath);
    }
    public MutilDownload(String urlPath, int threadPoolSize, String filePath)
            throws MalformedURLException {
        this(new URL(urlPath),threadPoolSize,filePath);
    }

    public void download()throws IOException{
        //URL url=new URL(urlPath);
        long contentLength=getContentLength();
        System.out.println("contentLength="+contentLength);
        long  left=contentLength%threadPoolSize;
        long  blockSize=contentLength/threadPoolSize;
        /*File file=new File(filePath);
        if(file.exists()){
            file.delete();
        }*/
        try(RandomAccessFile raf=new RandomAccessFile(filePath,"rw")){
            raf.setLength(contentLength);
        }
        long end;
        //tasks=new DownloadRunable[threadPoolSize];
        for(int i=0;i<threadPoolSize;i++){
            isArr[i]=url.openStream();
            end=(i==threadPoolSize-1?(i+1)*blockSize+left:(i+1)*blockSize);
            //tasks[i]=new DownloadRunable(i*blockSize,end,isArr[i],filePath)
            //executor.execute(tasks[i]);
            new Thread(new DownloadRunable(i*blockSize,end,isArr[i],filePath)).start();
        }
    }


    public long getContentLength() throws IOException {
        HttpURLConnection connection =(HttpURLConnection) url.openConnection();
        long contentLength=connection.getContentLength();
        connection.disconnect();
        return contentLength;
    }

/*    public boolean isCompleted(){
        while (true){
            for(int i=0;i<tasks.length;i++){
                if()
            }
        }
    }*/
}


